﻿@model Wheeler.Web.ViewModels.AddressViewModel
@using Wheeler.Common.Extensions;
@using Wheeler.Web.Utility;
@{    
    Wheeler.Domain.WheelerEntities context = new Wheeler.Domain.WheelerEntities();
    if(Model != null && Model.CityID > 0){
        Model.CityID = Model.CityID;
        
        Wheeler.Domain.City city = context.Cities.Include("State").FirstOrDefault(C => C.ID == Model.CityID);
        if(city != null){
            Model.StateID = city.StateID;
            Model.CountryID = city.State.CountryID;
        }
    }
    if(Model != null && Model.ZipCode == AppConstants.ValueOf.UNKNOWN){
        Model.ZipCode = String.Empty;
    }
    String _FieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    
    ///Here we found a nasty bug in ASP .NET MVC3, so had to do following ugly work-around.
    ///In the next release or ASP.NET MVC this bug should be resolved, then this code should be rewritten.
    ///http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#
    var _Countries = context.Countries.OrderBy(P => P.Name).ToList();
    var _States = (from P in context.States where P.CountryID == Model.CountryID orderby P.Name select P).ToList();
    var _Cities = (from P in context.Cities where P.StateID == Model.StateID orderby P.Name select P).ToList();
    var _AddressTypes = from Wheeler.Web.ViewModels.AddressType e in Enum.GetValues(typeof(Wheeler.Web.ViewModels.AddressType))
                select new { ID = (int)e, Name = e.ToString() };    
}   

	@Html.HiddenFor(model => model.ID)

	<div class="editor-label">
		@Html.LabelFor(model => model.StreetAddress)
	</div>
	<div class="editor-field">
		@Html.TextBoxFor(model => model.StreetAddress)
		@Html.ValidationMessageFor(model => model.StreetAddress)
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.Email)
	</div>
	<div class="editor-field">
		@Html.TextBoxFor(model => model.Email)
		@Html.ValidationMessageFor(model => model.Email)
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.Phone)
	</div>
	<div class="editor-field">
		@Html.TextBoxFor(model => model.Phone)
		@Html.ValidationMessageFor(model => model.Phone)
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.Fax)
	</div>
	<div class="editor-field">
		@Html.TextBoxFor(model => model.Fax)
		@Html.ValidationMessageFor(model => model.Fax)
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.ZipCode)
	</div>
	<div class="editor-field">
		@Html.TextBoxFor(model => model.ZipCode)
		@Html.ValidationMessageFor(model => model.ZipCode)
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.WebSite)
	</div>
	<div class="editor-field">
		@Html.TextBoxFor(model => model.WebSite)
		@Html.ValidationMessageFor(model => model.WebSite)
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.AddressType)
	</div>
	<div class="editor-field">
        @*We forced to bind dropdownlists as follows because of the following bug in ASP .NET MVC Model Binder
        http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#*@
        @Html.DropDownListFor(
            model => model.AddressType,
            new SelectList(_AddressTypes, "ID", "Name", Model.AddressType)
            , "Please Select")
		@Html.ValidationMessageFor(model => model.AddressType)
	</div>
    <div class="editor-label">
        @Html.LabelFor(model => model.CountryID)
	</div>
	<div class="editor-field">
	    @Html.DropDownListFor(
            model => model.CountryID, 
            new SelectList(_Countries, "ID", "Name", Model.CountryID)
            , "Please Select")
        @Html.ValidationMessageFor(model => model.CountryID)
	</div>    
    <div class="editor-label">
		@Html.LabelFor(model => model.StateID)
	</div>
	<div class="editor-field">
		@Html.DropDownListFor(
            model => model.StateID,
            new SelectList(_States, "ID", "Name", Model.StateID)
            , "Please Select")
         @Html.ValidationMessageFor(model => model.StateID)		
	</div>
	<div class="editor-label">
		@Html.LabelFor(model => model.CityID)
	</div>
	<div class="editor-field">
		@Html.DropDownListFor(
            model => model.CityID,
            new SelectList(_Cities, "ID", "Name", Model.CityID)
            , "Please Select")
		@Html.ValidationMessageFor(model => model.CityID)
	</div>
@*	@Html.HiddenFor(model => model.ModifiedBy)
	@Html.HiddenFor(model => model.CreatedOn)
	@Html.HiddenFor(model => model.ModifiedOn)*@


    <script language="javascript" type="text/javascript">
        var _Country = $('#@(_FieldPrefix)_CountryID');
        var _State = $('#@(_FieldPrefix)_StateID');
        var _City = $('#@(_FieldPrefix)_CityID');

        $(document).ready(function () {
            $(_Country).change(function () {
                Ajax('@Url.PublicUrl("GetStatesByCountry", "Ajax")'
                , 'countryID=' + $(this).val()
                , BindStates_Success);
                $(_City).find('option:gt(0)').remove();
            });
            $(_State).change(function () {
                Ajax('@Url.PublicUrl("GetCitiesByState", "Ajax")'
                , 'stateID=' + $(this).val()
                , BindCities_Success);
            });
        });
        function BindStates_Success(result) {
            var types = eval(result);
            BindDDL(types, _State);
        }
        function BindCities_Success(result) {
            var types = eval(result);
            BindDDL(types, _City);
        }
    </script>

					